/**
 * Copyright &copy; 2020 <a href="https://github.com/somowhere/exam-system">exam-system</a> All rights reserved.
 */
package com.shanhesoft.java.modules.exam.web;


import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.ApiOperation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanhesoft.java.common.core.constant.CommonConstants;
import com.shanhesoft.java.common.core.util.Result;
import com.shanhesoft.java.common.core.vo.PageModel;
import com.shanhesoft.java.common.core.vo.TreeNode;
import com.shanhesoft.java.common.data.util.QueryWrapperUtil;
import com.shanhesoft.java.common.web.resource.BaseResource;
import com.shanhesoft.java.modules.exam.domain.TBankFillQue;
import com.shanhesoft.java.modules.exam.domain.dto.CourseDto;
import com.shanhesoft.java.modules.exam.domain.dto.TBankFillQueDto;
import com.shanhesoft.java.modules.exam.domain.dto.TBankFillQueQueryCriteria;
import com.shanhesoft.java.modules.exam.domain.dto.TQuestionBankQueryCriteria;
import com.shanhesoft.java.modules.exam.service.CourseService;
import com.shanhesoft.java.modules.exam.service.TBankFillQueService;
import com.shanhesoft.java.modules.exam.service.TQuestionBankService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;

import javax.validation.Valid;
import java.util.*;

/**
 * 填空题管理Controller 填空题管理
 * @author admin
 * @version 2020-09-21 10:45:16
 */
@RestController
@RequestMapping(value = "${application.admin-path}/exam/bankFillQue")
@Slf4j
@AllArgsConstructor
public class TBankFillQueResource extends BaseResource {

	private final TBankFillQueService service;

	private final CourseService courseService;

	private final TQuestionBankService tQuestionBankService;

	/**
	 * @param id
	 * @return
	 */
	@GetMapping(CommonConstants.URL_ID_REGEX)
	@PreAuthorize("@pms.hasPermission('exam_tBankFillQue_view')")
	public Result get(@PathVariable String id) {
		log.debug("REST request to get Entity : {}", id);
		return  Result.buildOkData(service.getOneDto(id));
	}
	/**
	 * GET / : get all tBankFillQue.
	 *
	 * @param pm the pagination information
	 * @return the Result with status 200 (OK) and with body all tBankFillQue
	 */

	@PreAuthorize("@pms.hasPermission('exam_tBankFillQue_view')")
	@GetMapping
	@ApiOperation(value = "填空题管理查看")
	public Result getPage(PageModel pm, TBankFillQueQueryCriteria tBankFillQueQueryCriteria) {
		QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, tBankFillQueQueryCriteria);
		wrapper.orderByDesc("created_date");
		return Result.buildOkData(service.page(pm, wrapper));
	}

	@GetMapping("/getAll")
	@ApiOperation(value = "单选题按课程查询")
	public Result getAllPage(PageModel pm, @RequestParam("courseId") String courseId,
							 @RequestParam("content") String content, @RequestParam("publishFlag") String publishFlag){
		CourseDto courseDto = courseService.getOneDto(courseId);
		String bankId = courseDto.getBankId();
		// 根据题库ID查询当前及其下级ID
		TQuestionBankQueryCriteria tQuestionBankQueryCriteria = new TQuestionBankQueryCriteria();
		tQuestionBankQueryCriteria.setParentId(bankId);
		List<TreeNode> treeNodes = tQuestionBankService.findBankTreeNode(tQuestionBankQueryCriteria);
		Set<String> questionBankIds = new HashSet<String>();
		questionBankIds.add(bankId);
		for(TreeNode node : treeNodes) {
			questionBankIds.add(node.getId());
		}

		TBankFillQueQueryCriteria tBankFillQueQueryCriteria = new TBankFillQueQueryCriteria();
		tBankFillQueQueryCriteria.setQuestionBankIds(questionBankIds);
		if(null != content && !content.equals("")) {
			tBankFillQueQueryCriteria.setContent(content);
		}

		// 只有查已发布题目时需要带该参数，其它情况都是全部可选
		if(StringUtils.isNotEmpty(publishFlag)) {
			if("1".equals(publishFlag)) {
				tBankFillQueQueryCriteria.setPublishFlag(publishFlag);
			}
		}

		QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, tBankFillQueQueryCriteria);
		return Result.buildOkData(service.page(pm, wrapper));
	}

	/**
	 * POST / : Save a tBankFillQueDto.
	 *
	 * @param tBankFillQueDto the HTTP tBankFillQue
	 */
	@PreAuthorize("@pms.hasPermission('exam_tBankFillQue_edit')")
	@ApiOperation(value = "填空题管理编辑")
	@PostMapping
	public Result save(@Valid @RequestBody TBankFillQueDto tBankFillQueDto) {
		log.debug("REST request to save TBankFillQueDto : {}", tBankFillQueDto);
		service.saveOrUpdate(tBankFillQueDto);
		return Result.buildOk("保存填空题管理成功");

	}

	/**
	 * DELETE //:ids : delete the "ids" TBankFillQue.
	 *
	 * @param ids the id of the tBankFillQue to delete
	 * @return the Result with status 200 (OK)
	 */
	@PreAuthorize("@pms.hasPermission('exam_tBankFillQue_del')")
	@ApiOperation(value = "填空题管理删除")
	@DeleteMapping
	public Result delete(@RequestBody Set<String> ids) {
		log.debug("REST request to delete TBankFillQue: {}", ids);
		service.removeByIds(ids);
		return Result.buildOk("删除填空题管理成功");
	}

	@ApiOperation(value = "填空题Excel文件上传")
	@PostMapping(value = "/insertList")
	public Result insertList(@RequestBody Map<String, Object> obj){
		List<Map<String, Object>> list = (List<Map<String, Object>>) obj.get("params");
		List<TBankFillQue> bankList = new ArrayList<TBankFillQue>();
		for(Map<String, Object> map : list) {
			TBankFillQue bankFillQue = new TBankFillQue();
			bankFillQue.setQuestionBankId((String)map.get("questionBankId"));
			bankFillQue.setContent((String)map.get("content"));
			bankFillQue.setAnswer((String)map.get("answer"));
			bankFillQue.setAnswerExplain((String)map.get("answerExplain"));
			bankList.add(bankFillQue);
		}

		service.saveOrUpdateBatch(bankList);
		return Result.buildOk("文件上传成功");
	}
}
